package com.lv.fast.controller;

import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import com.github.xiaoymin.knife4j.annotations.ApiSupport;
import com.lv.fast.DTO.SysLoginDTO;
import com.lv.fast.constant.UserConstant;
import com.lv.fast.enums.RestResultEnum;
import com.lv.fast.model.RestResult;
import com.lv.fast.service.SysLoginService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Objects;
import java.util.Optional;

/**
 * @Description
 * @Author jie.lv
 */
@RestController
@AllArgsConstructor
@RequestMapping("/sys")
@Api(tags = "后台管理---登录模块")
@ApiSupport(author = "lv")
public class SysLoginController {

    private final SysLoginService sysLoginService;

    @PostMapping("/login")
    @ApiOperation(value = "登录")
    @ApiOperationSupport(order = 1)
    public RestResult login(@RequestBody @Validated SysLoginDTO loginDTO){
        Optional<String> token = sysLoginService.login(loginDTO);
        if (!token.isPresent()){
            return RestResult.build(RestResultEnum.LOGIN_FAIL);
        }
        if (Objects.equals(token.get(), UserConstant.JSON_KEY)){
            return RestResult.build(RestResultEnum.USER_LOCK);
        }
        return RestResult.success("token",token);
    }
}
